﻿using System;
using System.Runtime.Serialization;
using Microsoft.Kinect;

namespace TuDarmstadt.Informatik.TK.TnT.Captinect.Serializer
{
    [Serializable()]
    public class SerializableColorImageFrame : ISerializable
    {

        public SerializableColorImageFrame()
        {
        }

        public SerializableColorImageFrame(ColorImageFrame frame)
        {
            BytesPerPixel = frame.BytesPerPixel;
            Format = frame.Format;
            FrameNumber = frame.FrameNumber;
            Height = frame.Height;
            PixelDataLength = frame.PixelDataLength;
            Timestamp = frame.Timestamp;
            Width = frame.Width;
            ColorPixels = new byte[PixelDataLength];
            frame.CopyPixelDataTo(ColorPixels);
        }

        public SerializableColorImageFrame(SerializationInfo info, StreamingContext context)
        {
            BytesPerPixel = info.GetInt32("BytesPerPixel");
            Format = (ColorImageFormat)System.Enum.Parse(typeof(ColorImageFormat), info.GetString("Format"));
            FrameNumber = info.GetInt32("FrameNumber");
            Height = info.GetInt32("Height");
            Width = info.GetInt32("Width");
            PixelDataLength = info.GetInt32("PixelDataLength");
            Timestamp = info.GetInt64("Timestamp");
            ColorPixels = (byte[])info.GetValue("ColorPixels", typeof(byte[]));
        }

        public int BytesPerPixel { get; private set; }
        public ColorImageFormat Format { get; private set; }
        public int FrameNumber { get; private set; }
        public int Height { get; private set; }
        public int PixelDataLength { get; private set; }
        public long Timestamp { get; private set; }
        public int Width { get; private set; }
        public byte[] ColorPixels { get; private set; }

        public void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            info.AddValue("BytesPerPixel", BytesPerPixel);
            info.AddValue("Format", Format.ToString());
            info.AddValue("FrameNumber", FrameNumber);
            info.AddValue("Height", Height);
            info.AddValue("Width", Width);
            info.AddValue("PixelDataLength", PixelDataLength);
            info.AddValue("Timestamp", Timestamp);
            info.AddValue("ColorPixels", ColorPixels);
        }
    }
}
